home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #pragma once
-
- #include "stdlib.h"
- #include "OkTypes.h"
- #include "stream.h"
-
- // Temporary strings are generated by the concatenation operators.
- // They are designed to avoid calls to malloc, and as such are not
- // meant to be used directly.
-
- class OkStr;
-
- class OkTempStr {
- public:
- OkTempStr(OkTempStr const &other);
- ~OkTempStr();
-
- void destroy();
-
- // C++ uses these operators to perform the first concatenation
- friend OkTempStr operator|(OkStr const&, OkStr const&);
- friend OkTempStr operator|(OkStr const&, char const*);
- friend OkTempStr operator|(char const*, OkStr const&);
-
- // Susequent concatenations use these operators. These operators
- // just append the argument data to the temporary, avoiding malloc
- // when possible.
- // XXX these aren't really const, although they are declared as such
- // XXX to avoid warnings. In a land of very advanced compilers, this
- // XXX strategy may backfire.
- friend OkTempStr& operator|(const OkTempStr&, OkStr const& b);
- friend OkTempStr& operator|(const OkTempStr&, char const* b);
- friend ostream& operator<<(ostream& os, const OkTempStr& a)
- { os << (char*) a; return os; };
-
- operator char*() const
- { return data; }
- operator int() const
- { return atoi(data); }
- operator float() const
- { return float(atof(data)); }
- operator double() const
- { return double(atof(data)); }
-
- u_int length() const
- { return slength - 1; }
-
- protected:
- char indata[100]; // inline data, avoiding malloc
- char* data; // points at indata or heap
- u_int slength; // same rules as OkStr::slength
-
- OkTempStr(char const *, u_int, char const *, u_int);
-
- OkTempStr& concat(char const* b, u_int bl);
-
- friend class OkStr;
- };
-
- //----------------------------------------------------------------------
-
- class OkStr {
- friend class OkTempStr;
- public:
- OkStr(u_int l=0);
- OkStr(char const *s);
- OkStr(char const *s, u_int len);
- OkStr(OkStr const&);
- OkStr(char *s, char const* format);
- OkStr(int, char const* format);
- OkStr(float, char const* format);
- OkStr(double, char const* format);
- OkStr(const OkTempStr&);
-
- ~OkStr();
- void destroy();
-
- u_long hash() const;
-
- operator char*() const
- { return data; }
- operator int() const
- { return atoi(data); }
- operator float() const
- { return float(atof(data)); }
- operator double() const
- { return double(atof(data)); }
-
- u_int length() const { return slength-1; }
-
- char& operator[](u_int i) const
- { assert(i<slength-1);
- return data[i]; }
-
- void operator=(const OkTempStr& s);
- void operator=(OkStr const& s);
- void operator=(char const *s);
-
- friend ostream& operator<<(ostream& os, const OkStr& a)
- { os << (char*) a; return os; };
-
- /////////////////////////////////////////////////////
- // Comparison
- friend OkBool operator==(OkStr const&, OkStr const&);
- friend OkBool operator==(OkStr const&, char const*);
- friend OkBool operator==(char const*, OkStr const&);
-
- friend OkBool operator!=(OkStr const&, OkStr const&);
- friend OkBool operator!=(OkStr const&, char const*);
- friend OkBool operator!=(char const*, OkStr const&);
-
- friend OkBool operator>=(OkStr const&, OkStr const&);
- friend OkBool operator>=(OkStr const&, char const*);
- friend OkBool operator>=(char const*, OkStr const&);
-
- friend OkBool operator<=(OkStr const&, OkStr const&);
- friend OkBool operator<=(OkStr const&, char const*);
- friend OkBool operator<=(char const*, OkStr const&);
-
- friend OkBool operator>(OkStr const&, OkStr const&);
- friend OkBool operator>(OkStr const&, char const*);
- friend OkBool operator>(char const*, OkStr const&);
-
- friend OkBool operator<(OkStr const&, OkStr const&);
- friend OkBool operator<(OkStr const&, char const*);
- friend OkBool operator<(char const*, OkStr const&);
-
- int compare(OkStr const *a) const { return ::compare(*this, *a); }
- friend int compare(OkStr const&, OkStr const&);
- friend int compare(OkStr const&, char const*);
- friend int compare(char const*, OkStr const&);
-
- /////////////////////////////////////////////////////
- // Concatenation
- friend OkTempStr& operator|(const OkTempStr&, OkStr const&);
- friend OkTempStr& operator|(const OkTempStr&, char const*);
-
- friend OkTempStr operator|(OkStr const&, OkStr const&);
- friend OkTempStr operator|(OkStr const&, char const*);
- friend OkTempStr operator|(char const*, OkStr const&);
-
- /////////////////////////////////////////////////////
- // Misc
- OkStr copy() const;
- OkStr extract(u_int start,u_int len) const;
- OkStr cut(u_int start,u_int len);
- OkStr head(u_int);
- OkStr tail(u_int);
- void lower();
- void raise();
-
- u_int cnt(char a);
-
- void remove(u_int posn,u_int len=1);
-
- void resize(u_int len, OkBool reallocate = FALSE);
- void setMaxLength(u_int maxlen);
-
- void append(char a);
- void append(char const *s, u_int len=0);
- void append(const OkTempStr& s)
- { append((char*)s, s.slength-1); }
- void append(OkStr const& s)
- { append((char*)s, s.slength-1); }
-
- void insert(char a, u_int posn=0);
- void insert(char const *, u_int posn=0, u_int len=0);
- void insert(const OkTempStr& s, u_int posn=0)
- { insert((char*)s, posn, s.slength-1); }
- void insert(OkStr const& s, u_int posn=0)
- { insert((char*)s, posn, s.slength-1); }
-
- /////////////////////////////////////////////////////
- // Parsing
- u_int next(u_int posn, char delimiter) const;
- u_int next(u_int posn, char const *delimiters, u_int len=0) const;
- u_int next(u_int posn, OkStr const& delimiters) const
- { return next(posn, (char*)delimiters, delimiters.slength-1); }
-
- u_int nextR(u_int posn, char delimiter) const;
- u_int nextR(u_int posn, char const*, u_int len=0) const;
- u_int nextR(u_int posn, OkStr const& delimiters) const
- { return nextR(posn, (char*)delimiters, delimiters.slength-1); }
-
- u_int skip(u_int posn, char a) const;
- u_int skip(u_int posn, char const *, u_int len=0) const;
- u_int skip(u_int posn, OkStr const& delimiters) const
- { return skip(posn, (char*)delimiters, delimiters.slength-1); }
-
- u_int skipR(u_int posn, char a) const;
- u_int skipR(u_int posn, char const *, u_int len=0) const;
- u_int skipR(u_int posn, OkStr const& delimiters) const
- { return skipR(posn, (char*)delimiters, delimiters.slength-1); }
-
- OkStr token(u_int & posn, char delimiter) const;
- OkStr token(u_int & posn, char const * delimiters,
- u_int delimiters_len = 0) const;
- OkStr token(u_int & posn, OkStr const & delimiters) const
- { return token(posn, delimiters.data, delimiters.slength-1); }
- // same as token() except it returns a null token when
- // there are two delimiters adjacent to each other
- OkStr ptoken(u_int & posn, char delimiter) const;
-
- OkStr tokenR(u_int & posn, char delimiter) const;
- OkStr tokenR(u_int & posn, char const * delimiters,
- u_int delimiters_len = 0) const;
- OkStr tokenR(u_int & posn, OkStr const & delimiters) const
- { return tokenR(posn, delimiters.data, delimiters.slength-1); }
-
- protected:
- // slength is one greater than the true length of the data.
- // This is because the data is null-terminated. However, the
- // data may contain nulls; they will be ignored. This is to
- // provide compatibility with ordinary C-style strings, and
- // with arbitrary data. slength is always positive.
- u_int slength;
-
- // data points to the actual data. It is always a valid pointer.
- char * data;
-
- // zero-length string support
- // resizeInternal doesn't update slength or handle null termination
- static char OkStr::emptyString;
- void OkStr::resizeInternal(u_int);
-
- int findEndBuffer(const char *, u_int buflen) const;
- int findBuffer(const char *buf, u_int buflen) const;
- void bracketBuffer(const char *, u_int buflen, int &, int &) const;
- };
-
-